<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>微信企业号-应用消息推送</title>
    <link rel="stylesheet" href="/css/zui.min.css">
</head>
<body>
[#if status??]
<div>
    <h4>${error_message}</h4>
</div>
[#else]
<div class="container-fixed-md">
    <ol class="breadcrumb" style="margin-top: 15px;margin-bottom: 0px;">
        <li><a href="/"><i class="icon icon-home"></i> 首页</a></li>
        <li class="active">群聊会话</li>
        <div class="pull-right text-primary">
            <a href="/chats-create?agent_id=${agent.agentId}"><i class="icon icon-plus"></i> 新建群聊</a>
        </div>
    </ol>
    <hr style="margin-top: 0px; margin-bottom: 0px;">
</div>

<div class="container-fixed-md">
    [#list chats as chat]
    <div class="items items-hover">
        <div class="item">
            <div class="item-heading">
                <h4><a href="###">${chat.name}</a></h4>
            </div>
            <div class="item-content">
                <div class="text text-ellipsis">
                    成员：
                    [#list chat.userlist as user]
                    ${user}&nbsp;&nbsp;
                    [/#list]
                </div>
            </div>
            <div class="item-footer">
                <i class="icon icon-user"></i> 群主：<span class="text-muted">${chat.owner}</span>
                <div class="pull-right" style="margin: 0 15px;">
                    <a href="/chats-message?agent_id=${agent.agentId}&chat_id=${chat.chatid}">
                        <i class="icon icon-comments-alt text-primary icon-2x"></i>
                    </a>
                </div>
            </div>
        </div>
    </div>
    [/#list]
</div>


<script src="/js/jquery1.8.1.js"></script>
<script src="/js/zui.min.js"></script>
<script type="text/javascript">
    // 序列化JSON字符串
    $.fn.serializeObject = function () {
        let o = {};
        let a = this.serializeArray();
        $.each(a, function () {
            let value = this.value;
            let chain = this.name.split(".");
            let c = o;
            $.each(chain, function (i, e) {
                if (i === (chain.length - 1)) {
                    if (c[e]) {
                        if (!c[e].push) {
                            c[e] = [c[e]];
                        }
                        c[e].push(value || '');
                    } else {
                        c[e] = value || '';
                    }
                } else {
                    if (!c[e]) {
                        c[e] = {};
                    }
                }
                c = o[e];
            });
        });
        return o;
    };

    var agent_id = '${agent.agentId}';
    var sendMessage = function (msg) {
        if (agent_id == null || agent_id == '' || agent_id == undefined) {
            alert("应用ID不能为空");
            return false;
        }
        $.ajax({
            url: '/message?agent_id=' + agent_id,
            type: 'POST',
            dataType: 'json',
            contentType:"application/json",
            data: msg,
            beforeSend: function () {
                console.log(agent_id + ' start send message');
            },
            success: function (result) {
                console.log(result);
                if (result.status == 'ok') {
                    new $.zui.Messager('  提示消息：成功!', {
                        type: 'success',
                        time: 3000
                    }).show();
                } else {
                    new $.zui.Messager('  提示消息：' + result.status + '!', {
                        type: 'danger',
                        time: 3000
                    }).show();
                }
            },
            error: function () {
                new $.zui.Messager('  提示消息：请求失败!', {
                    type: 'danger',
                    time: 5000
                }).show();
            },
            complete: function () {

            }
        });
    }

    $('#text-message-btn').on('click', function () {

        var form = $('#text-message-form').serializeObject();
        if($('#safe').is(':checked')){
            form.safe = 1;
        } else {
            form.safe = 0;
        }
        console.log(form);
        sendMessage(JSON.stringify(form));
    });
</script>
[/#if]
</body>
</html>